lectures.alex.balgavy.eu

Lecture notes from university.
git clone git://git.alex.balgavy.eu/lectures.alex.balgavy.eu.git
Log | Files | Refs | Submodules

The Standard Library.md (2223B)


      1 +++
      2 title = 'The Standard Library'
      3 +++
      4 # The Standard Library
      5 ## The minimal program
      6 ```cpp
      7 int main() {}
      8 ```
      9 
     10 Every program needs a main method. A nonzero return value indicates failure.
     11 
     12 ## Hello world
     13 ```cpp
     14 #include <iostream>
     15 int main() {
     16     std::cout << “Hello, world!\n”;
     17 }
     18 ```
     19 
     20 ## The standard library namespace
     21 
     22 Everything provided by the standard library needs a `std::` prefix. Either include headers and use prefix, or make everything global, such as this:
     23 
     24 ```cpp
     25 #include <string> // include the standard string facilities
     26 using namespace std; // make std names available without prefix
     27 string s=“Ignorance is bliss!” // cool, string is std::string
     28 ```
     29 
     30 But making everything global is generally in poor taste. Albeit easier.
     31 
     32 ## Output
     33 By default, values to output are converted to a sequence of characters.
     34 You can combine values in an obvious way:
     35 
     36 ```cpp
     37 void f(int i) {
     38     cout << “The value of i is “;
     39     cout << i;
     40     cout << “.\n”;
     41 }
     42 ```
     43 
     44 To simplify it, you can use the result of an output expression for further output:
     45 
     46 ```cpp
     47 void g(int i) {
     48     cout << “The value of i is “ << i << “.\n”;
     49 }
     50 ```
     51 
     52 ## Strings
     53 the standard library provides string concatenation, using the + operator.
     54 
     55 ```cpp
     56 string s3 = “Hello “ + “world.” + “\n”;
     57 cout << s3;
     58 ```
     59 
     60 you can also append to a string:
     61 
     62 ```cpp
     63 s1 = s1 + “\n”;    // verbose
     64 s1 += “\n”;        // shorthand
     65 ```
     66 
     67 manipulating substrings is also straightforward:
     68 
     69 ```cpp
     70 string name=“Niels Stroustrup”;
     71 
     72 string s=name.substr(6,10);    // s=“Stroustrup” — starting at 6, with a len of 10
     73 
     74 name.replace(0,5,”Nicholas”);  // name becomes “Nicholas Stroustrup”
     75 ```
     76 
     77 ## Input
     78 ```cpp
     79 int main() {
     80     string str;
     81     cout << “Please enter your name:\n”;
     82     cin >> str;
     83     cout << “Hello “ << str << “!\n”;
     84 }
     85 ```
     86 
     87 ## Vectors
     88 ```cpp
     89 struct Entry {
     90     string name;
     91     int number;
     92 }
     93 
     94 vector<Entry> phone_book(1000);    // Initial size: 1000
     95 void print_entry(int i) {
     96     cout << phone_book[i].name << ‘ ‘ << phone_book[i].number << ‘\n’;
     97 }
     98 void add_entries(int n) {
     99     phone_book.resize(phone_book.size()+n);
    100 }
    101 ```